package com.lucas.boot;

import com.lucas.pojo.RpcConsumer;
import com.lucas.service.UserService;
import com.lucsa.pojo.RpcRequest;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;

import java.util.List;
import java.util.Random;


public class BootStrap {
    private static RpcRequest providerRpcRequest= new RpcRequest();

    //旧版本(案例版本)
//    private static String provider = "userService#sayHello";



//    private static String provider;

    public static void main(String[] args) throws InterruptedException {

        //远程调用者(工具类):把想要代理的类交给RpcConsumer 就会返回一个代理类.
        RpcConsumer rpcConsumer = new RpcConsumer();

        /**
         * 构建信息格式体
         */
        //需要调用userService类
        providerRpcRequest.setClassName("com.lucas.Service.UserServiceImpl");
        //调用sayHello方法
        providerRpcRequest.setMethodName("sayHello");
        //需要传递参数类型
        providerRpcRequest.setParameterTypes(new Class[]{Object.class});
        //需要参数的数值
        providerRpcRequest.setParameters(new Object[]{"1"});
        //requestId暂时不知要要干嘛.做唯一区分.
        providerRpcRequest.setRequestId(new Random().toString());

        //构建zookeeper instance
        System.out.println("客户端zookeeper 通信建立来了");
        ZkClient zkClient = new ZkClient("192.168.162.129:2181");
        String  ip =(String) zkClient.readData("/zookepperConfig/host1/ip");
        String port =(String)zkClient.readData("/zookepperConfig/host1/port");
        //构建一个rpc的代理类,就是若本环境访问 userService.则就是远程访问Uservice的sayHello方法.
        UserService o = (UserService)rpcConsumer.creatProxy(UserService.class,ip,port);

        //注册一个监听
        zkClient.subscribeChildChanges("/zookepperConfig/host1", new IZkChildListener() {
            @Override
            public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
                System.out.println("子节点发生变化,parentPath:"+parentPath+" 其子节点为currentChilds"+currentChilds);
                String ip = (String)zkClient.readData("/zookepperConfig/host1/ip");
                String port = (String)zkClient.readData("/zookepperConfig/host1/port");
                if (ip!=null && port !=null) {
                    //这个方法上面出现了问题
                    UserService userService = (UserService)new RpcConsumer().creatProxy(UserService.class,ip,port);
                    for(;;){
                        System.out.println("====客户端开始准备发送消息2=====");
                        //s就是调用远程rpc的结果.代码是就是使用多线程调用call方法
                        String s = userService.sayHello(providerRpcRequest);
                        System.out.println(s);
                        Thread.sleep(2000);
                    }
                }
            }
        });


        //循环调用
        for(;;){
            System.out.println("====客户端开始准备发送消息=====");
            //s就是调用远程rpc的结果.代码是就是使用多线程调用call方法
            String s = o.sayHello(providerRpcRequest);
            System.out.println(s);
            Thread.sleep(2000);
        }
    }
}
